package com.indianindie;

import java.util.Iterator;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

public class SRGameRenderer implements Renderer {
	
    SRTextureComponent textureComponent;
	SRGamePhysicsLoop physicsLoop = new SRGamePhysicsLoop();
	Context context;

	public SRGameRenderer(Context context) {
		super();
		this.context = context;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		SRGameLevelLoader levelLoader = new SRGameLevelLoader();
		physicsLoop.gameDisplayComponents = levelLoader.init();
		textureComponent = new SRTextureComponent(gl, 1);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		
		//gl.glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		
		gl.glLoadIdentity();
		
//		gl.glTranslatef(1.0f, -1.0f, 0.0f);
//	    gl.glRotatef(90, 0.0f, 0.0f, 1.0f);
//	    gl.glScalef(2.0f/(float)SRGameDisplayObject.DISPLAY_BLOCK_WIDTH, 2.0f/(float)SRGameDisplayObject.DISPLAY_BLOCK_HEIGHT, 1.0f);
//	        
		Iterator<SRGameDisplayObject> it = physicsLoop.gameDisplayComponents.iterator();
		while (it.hasNext()) {
			textureComponent.init(gl);
			
			textureComponent.loadTexture(gl, context, it.next());
		}
	
		
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		gl.glViewport(0, 0, width,height);
		 
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		
		gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
		
	}

	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		gl.glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
		Log.i("elements",physicsLoop.gameDisplayComponents.size()+"");
		Iterator<SRGameDisplayObject> it = physicsLoop.gameDisplayComponents.iterator();
		while (it.hasNext()) {
			textureComponent.draw(gl, it.next());
		}
		
	}
}
